// mytablemodel.cpp
#include "ctmaintablemodel.h"

CtMainTableModel::CtMainTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    m_rowCheckedStates.resize(m_diVect.size());
    m_headList<<"Sel"<<"ID"<<"域名"<<"后缀"<<"注册状态"<<"上次查询日期"<<"注册商名称"<<"创建时间"<<"过期时间"<<"更新时间"<<"DNS服务器"<<"所有人名称"<<"所有人组织"<<"所有人国家"<<"所有人邮箱"<<"域名状态";
}

CtMainTableModel::~CtMainTableModel()
{
}

int CtMainTableModel::rowCount(const QModelIndex &parent) const
{
    if ( parent.isValid() )
        return 0;
    return m_diVect.size();
}

int CtMainTableModel::columnCount(const QModelIndex &parent) const
{
    if ( parent.isValid() )
        return 0;
    return m_headList.size();
}

QVariant CtMainTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int nRow = index.row();
    int nColumn = index.column();
    DnSummaryInfo dnsi = m_diVect[nRow];
    //qDebug() << nRow<< nColumn << "role:" << role;
    if (nColumn == 0 && role == Qt::CheckStateRole) {
        return dnsi.ifChecked ? Qt::Checked : Qt::Unchecked;
    }else if(role == Qt::DisplayRole)
    {
    switch (nColumn) {
    // case 0:
    //     qDebug()<<"122"<<role;
    //     if(role == Qt::CheckStateRole)
    //         return Qt::Unchecked;
    case 1:
        return QString::number(dnsi.id);
    case 2:
        return dnsi.domain;
    case 3:
        return dnsi.suffix;
    case 4:
        return dnsi.registStatus;
    case 5:
        return dnsi.lastQryDate;
    case 6:
        return dnsi.registrar;
    case 7:
        return dnsi.creationDate;
    case 8:
        return dnsi.expiryDate;
    case 9:
        return dnsi.updatedDate;
    case 10:
        return dnsi.nameServer;
    case 11:
        return dnsi.registrant;
    case 12:
        return dnsi.registrantOrg;
    case 13:
        return dnsi.registrantCountry;
    case 14:
        return dnsi.registrantEmail;
    case 15:
        return dnsi.domainStatus;
    default:
        return QVariant();
    }}
     return QVariant();
}

bool CtMainTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    int row = index.row();
    if (row < 0 || row >= m_diVect.size())
    {
        qDebug()<<row<<m_diVect.size();
        return false;
    }
    if (role == Qt::CheckStateRole && index.column() == 0) {
        int state = value.toInt();
        m_diVect[row].ifChecked = state== Qt::Checked;
        emit dataChanged(index, index, {role});
        return true;
    }else if( index.column()>0)
    {
        DnSummaryInfo &dnsi = m_diVect[row];
        switch (index.column()) {
        case 0:
            //dnsi.id = value.toInt();
        qDebug()<<1111;
            break;
        case 1:
            dnsi.id = value.toInt();
            break;
        case 2:
            dnsi.domain = value.toString();
            break;
        case 3:
            dnsi.suffix = value.toString();
            break;
        case 4:
            dnsi.registStatus = value.toString();
            break;
        case 5:

            dnsi.lastQryDate = value.toString();
            break;
        case 6:
            dnsi.registrar = value.toString();
            break;
        case 7:
            dnsi.creationDate = value.toString();
            break;
        case 8:
            dnsi.expiryDate = value.toString();
            break;
        case 9:
            dnsi.updatedDate = value.toString();
            break;
        case 10:
            dnsi.nameServer = value.toString();
            break;
        case 11:
            dnsi.registrant = value.toString();
            break;
        case 12:
            dnsi.registrantOrg = value.toString();
            break;
        case 13:
            dnsi.registrantCountry = value.toString();
            break;
        case 14:
            dnsi.registrantEmail = value.toString();
            break;
        case 15:
            dnsi.domainStatus = value.toString();
            break;
        default:
            return false;
        }
    }
    //    Qt::CheckState checkState = value.value<Qt::CheckState>();
    //    if (checkState == Qt::Checked)
    //        m_rowCheckedStates[row] = true;
    //    else if (checkState == Qt::Unchecked)
    //        m_rowCheckedStates[row] = false;

    //emit dataChanged(index, index);
    return true;
}

Qt::ItemFlags CtMainTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    if (index.column() == 0) {
        // 第一列可被选中
        Qt::ItemFlags flag = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        //qDebug() << "flags(): index.column():" << index.column() << "flags:" << flag;
        return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    } else {
        // 其他列只读
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }
}

QVariant CtMainTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        switch (section) {
        case 0:
            return tr("");
        case 1:
            return tr("ID");
        case 2:
            return tr("域名");
        case 3:
            return tr("后缀");
        case 4:
            return tr("注册状态");
        case 5:
            return tr("上次查询日期");
        case 6:
            return tr("注册商名称");
        case 7:
            return tr("创建时间");
        case 8:
            return tr("过期时间");
        case 9:
            return tr("更新时间");
        case 10:
            return tr("DNS服务器");
        case 11:
            return tr("所有人名称");
        case 12:
            return tr("所有人组织");
        case 13:
            return tr("所有人国家");
        case 14:
            return tr("所有人邮箱");
        case 15:
            return tr("域名状态");
        default:
            return QVariant();
        }
    }
    if (orientation == Qt::Vertical && role == Qt::DisplayRole)
        return QVariant::fromValue(section + 1); // 对应于行标题

    if (orientation == Qt::Vertical && role == Qt::CheckStateRole)
        return QVariant(m_rowCheckedStates[section] ? Qt::Checked : Qt::Unchecked); // 对应于行的勾选状态

    return QAbstractTableModel::headerData(section, orientation, role);
}

QVariant CtMainTableModel::verticalHeaderData(int section, int role) const
{
    qDebug()<<section;
    if (role == Qt::DisplayRole || role == Qt::ToolTipRole || role == Qt::StatusTipRole)
        return QVariant();

    return section + 1; // 显示行号，从1开始
}

void CtMainTableModel::addData(const DnSummaryInfo &dnsi)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_diVect.append(dnsi);
    endInsertRows();
}

void CtMainTableModel::deleteData(const int row)
{
    if (row < 0 || row >= m_diVect.size())
        return;

    beginRemoveRows(QModelIndex(), row, row);
    m_diVect.removeAt(row);
    endRemoveRows();
}

void CtMainTableModel::clearData()
{
    beginResetModel();
    m_diVect.clear();

    emit dataCleared();
    endResetModel();
}

void CtMainTableModel::updateRangeData(QModelIndex topLeftIndex, QModelIndex bottomRightIndex)
{
    emit dataChanged(topLeftIndex, bottomRightIndex);
}

int  CtMainTableModel::findItemsIdxByDnId(const int dnid ) const
{
    for (int row = 0; row < rowCount(); row++)
    {
        QModelIndex index = this->index(row, 0);//只对第0列的domainId查询
        int  data = this->data(index, Qt::DisplayRole).toInt();
        if(data==dnid)
            return row;
    }
    return -1;
}
void CtMainTableModel::toggleAllSelected(bool checked) {

    for (int row = 0; row < rowCount(); row++) {
        m_diVect[row].ifChecked = checked;
    }
    emit dataChanged(index(0, 0), index(rowCount() - 1, 0), {Qt::CheckStateRole});
}
